package com.zy.db.odbc;

import com.zy.common.core.exception.BizException;
import com.zy.db.enums.DbErrorEnums;
import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

@Slf4j
public class OdbcPool implements OdbcSource {


    private static Map<String, Connection> connectionMap = new HashMap<>();


    @Override
    public Connection getConnection() throws SQLException {
        return null;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public Connection getConnection(String url, String username, String password) throws SQLException {
        Connection ct = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            DriverManager.setLoginTimeout(3);
            ct = DriverManager.getConnection(url, username, password);
            if (!ct.isClosed()) {
                connectionMap.put(url+username+username, ct);
            } else {
                throw new BizException(DbErrorEnums.ODBC_CONNECTION_ERROR);
            }
        } catch (Exception e) {
            StringBuffer sb = new StringBuffer(DbErrorEnums.ODBC_CONNECTION_ERROR.getDesc() + "=>");
            sb.append("\n");
            sb.append("Url:" + url);
            sb.append("\n");
            sb.append("userName:" + username);
            sb.append("\n");
            sb.append("password:" + password);
            sb.append("\n");
            sb.append(e.getMessage());
            log.info(sb.toString());
            throw new BizException(DbErrorEnums.ODBC_CONNECTION_ERROR);
        }
        return ct;
    }

    @Override
    public Connection getConnection(String url) {
        return connectionMap.get(url);
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }


}
